package com.ftdigital.primemovers_operators.fragment;

import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.MyApplication;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.model.BaseModel;
import com.ftdigital.primemovers_operators.model.ListEntity;
import com.ftdigital.primemovers_operators.model.Order;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;
import com.ftdigital.primemovers_operators.util.http.RetryHandler;
import com.ftdigital.primemovers_operators.util.view.RoundImageView;
import com.google.gson.JsonSyntaxException;
import com.squareup.picasso.Picasso;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.ViewsById;

import java.util.List;

/**
 * 交易评价
 */
@EFragment(R.layout.fragment_trading_evaluation)
public class TradingEvaluationFragment extends BaseFragment {
    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.ratingBar)
    RatingBar ratingBar;

    @ViewById(R.id.headimg)
    RoundImageView headimg;

    @FragmentArg("listEntity")
    ListEntity listEntity;

    @ViewsById({R.id.username, R.id.userno, R.id.car_num, R.id.oil_type, R.id.station_name,
            R.id.dealing_addtype_content, R.id.input_oil_L_hint_content, R.id.input_oil_thisprice_price_content})
    List<TextView> detailList;

    @ViewById(R.id.detail_content)
    EditText detailContent;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_trading_evaluation));
        ratingBar.getProgress();
        loadData();
    }

    private void loadData() {
        Picasso.with(getActivity()).load(MyApplication.userInfo.getData().getUrl()).error(R.drawable.user_headimg).into(headimg);
        detailList.get(0).setText(MyApplication.userInfo.getData().getUser_name());
        detailList.get(1).setText(MyApplication.userInfo.getData().getEmp_no());
        detailList.get(2).setText(listEntity.getCar_num());
        detailList.get(3).setText(listEntity.getCar_type());
        detailList.get(4).setText(MyApplication.userInfo.getData().getGasstation_name());
        detailList.get(5).setText(listEntity.getType());
        detailList.get(6).setText(listEntity.getQty());
        detailList.get(7).setText(listEntity.getPrice()+"");
    }

    @Click({R.id.back_btn, R.id.submit_btn})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
            case R.id.submit_btn:
                if (detailContent.getText().toString().trim().equals("")){
                    Toast.makeText(getActivity(), "请添加评论哦", Toast.LENGTH_SHORT).show();
                    return;
                }
                submitData();
                break;
        }
    }
    private void submitData() {
        requestParams = "cooperattitude="
        +"&detail="+detailContent.getText().toString().trim()
                +"&iscooerate="
                +"&issmooth="
                +"&order_id="+listEntity.getOrder_id()
                +"&total="+ratingBar.getProgress()
                +"&user_id="+MyApplication.userInfo.getData().getUser_id();
        requestUrl = RequestAddr.addEvaluation + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    private void dealData(Object object) {
        try {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
        } catch (JsonSyntaxException e) {
            return;
        }
        Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
        if (MainActivity.getInstance().getSupportFragmentManager().getBackStackEntryCount() == 1&&MainActivity.tabHost.getCurrentTab() ==1) {
            MainActivity.tabHost.setCurrentTab(0);
            MainActivity.tabHost.setCurrentTab(1);
        }
        MainActivity.getInstance().getSupportFragmentManager().popBackStack();
    }
}
